package com.usermanage.ServiceImpl;

import com.usermanage.Mapping.ProjectMapping;
import com.usermanage.Service.ProjectService;
import com.usermanage.beanParam.ProjectBean;
import com.usermanage.dataDao.JpaDao;
import com.usermanage.util.KingDeeUtil;
import com.usermanage.util.KingdeeStatic;
import com.usermanage.util.StaticUtil;
import com.usermanage.util.StringUtil;
import kingdee.bos.webapi.client.K3CloudApiClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service("projectService")
public class ProjectServiceImpl implements ProjectService{

    @Resource
    private ProjectMapping projectMapping;
    @Resource
    private JpaDao jpaDao;

    @Override
    public ProjectBean save(ProjectBean projectBean) {
        K3CloudApiClient client = KingDeeUtil.login(StaticUtil.DATASOURCE);
        String code = projectBean.getProject_code();
        String name = projectBean.getProject_name();
        Map result = new HashMap();
        Map model = new HashMap();
        model.put("FNumber", code);
        model.put("FDataValue", name);
        Map fid = new HashMap();
        fid.put("FNumber", "PJ002");
        model.put("FId", fid);
        result.put("Model", model);
        Map saveMap = KingDeeUtil.save(client, KingdeeStatic.BOS_ASSISTANTDATA_DETAIL, StringUtil.ObjectToJson(result));
        log.info("金蝶项目保存结果："+StringUtil.ObjectToJson(saveMap));
        Map submitMap = KingDeeUtil.submit(client,KingdeeStatic.BOS_ASSISTANTDATA_DETAIL,code);
        log.info("金蝶项目提交结果："+StringUtil.ObjectToJson(submitMap));
        Map auditMap = KingDeeUtil.audit(client,KingdeeStatic.BOS_ASSISTANTDATA_DETAIL,code);
        log.info("金蝶项目审核结果："+StringUtil.ObjectToJson(auditMap));

        return projectMapping.save(projectBean);
    }

    @Override
    public Optional<ProjectBean> findById(Integer id) {
        return projectMapping.findById(id);
    }

    @Override
    public List<ProjectBean> findAllList(String key, String page, String size,String company_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * FROM project where 1=1 ");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and project_name like '%").append(key).append("%'");
        }
        if (StringUtils.isNotBlank(company_id)){
            buffer.append(" and company_id = '").append(company_id).append("'");
        }
        if(Integer.valueOf(size)>0){
            buffer.append(" and project_status = '00' order by id desc limit ").append((Integer.valueOf(page)-1) * Integer.valueOf(size)).append(",").append(size);
        }
        List<ProjectBean> list = jpaDao.getListByPage(buffer.toString(),ProjectBean.class);
        return list;
    }

    @Override
    public int getListCount(String key,String company_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(*) FROM project where 1=1 and project_status = '00'");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and project_name like '%").append(key).append("%'");
        }
        if (StringUtils.isNotBlank(company_id)){
            buffer.append(" and company_id = '").append(key).append("'");
        }
        return jpaDao.getListCountByPage(buffer.toString());
    }

    @Override
    public Boolean isExisit(String project_code,Integer id) {
        ProjectBean projectBean = new ProjectBean();
        projectBean.setProject_code(project_code);
        if(id == null){
            Example<ProjectBean> example = Example.of(projectBean);
            return projectMapping.exists(example);
        }else{
           int count = projectMapping.checkExist(project_code,id.toString());
           if(count>0){
               return true;
           }else{
               return false;
           }
        }
    }

    /**
     * 渠道商项目列表
     *
     * @param company_id
     * @return
     */
    @Override
    public List<Map> projectListByCompany(String company_id) {
        if("1".equals(company_id)){
            company_id="";
        }
        List<Map> projectListByCompany = new ArrayList<>();
        List<ProjectBean> projectBeanList = findAllList(null,"0","0",company_id);
        for (int i = 0; i < projectBeanList.size(); i++) {
            Map map = new HashMap();
            map.put("project_name", projectBeanList.get(i).getProject_name());
            projectListByCompany.add(map);
        }
        return projectListByCompany;
    }
}
